#include "instructions/base/class_init_logic.hpp"


namespace jvm{
	namespace instructions {
		void scheduleClinit(std::shared_ptr<rtda::Thread> pthread, std::shared_ptr<heap::Class> pclass)
		{
			auto clinit = pclass->getClinitMethod();
			if(clinit && clinit->getClass() == pclass)
			{
				// exec <clinit>
				auto newFrame = pthread->newFrame(clinit);
				pthread->pushFrame(newFrame);
			}
		}

		void initSuperClass(std::shared_ptr<rtda::Thread> pthread, std::shared_ptr<heap::Class> pclass) 
		{
			if(pclass->isInterface())
			{
				auto superClass = pclass->getSuperClass();
				if (superClass != nullptr && !superClass->getInitStarted()) {
					initClass(pthread, superClass);
				}
			}
		}


		void initClass(std::shared_ptr<rtda::Thread> pthread, std::shared_ptr<heap::Class> pclass)
		{
			pclass->InitStarted();
			scheduleClinit(pthread, pclass);
			initSuperClass(pthread, pclass);
		}

	}// instructions
}// namespace jvm
